const router = require('koa-router')()
const multer = require('koa-multer')
const config = require('../config/common_cfg')
const checkToken = require('../middlewares/checkToken')
const UserControl = require('../controllers/user.controller'),
    Article = require('../controllers/article.controller'),
    Upload = require('../controllers/upload.controller')

router.prefix(config.app.routerBaseApi)

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'public/upload/article/images')
    },
    filename: (req, file, cb) => {
        const fileFormat = (file.originalname).split(".");  //以点分割成数组，数组的最后一项就是后缀名
        cb(null,Date.now() + "." + fileFormat[fileFormat.length - 1]);
    }
})
const upload = multer({ storage: storage })
router.get('/login', UserControl.login)
    .get('/getUserInfo', UserControl.getUserInfo)
    .get('/getAllUserInfo', UserControl.getAllUserInfo)
    .get('/getAllArticles', Article.getAllArticles)
    .get('/getPublishedArticles', Article.getPublishedArticles)
    .get('/getOneArticleContent', Article.getOneArticleContent)
    .post('/publishArticle', checkToken, Article.publishArticle)
    .post('/addOneArticle',checkToken, Article.addOneArticle)
    .post('/upImg', checkToken, upload.single('file'), Upload.upImg)

module.exports = router
